package main

func main() {
	println(romanToInt("DCXXI"))
}

func romanToInt(s string) int {
	result := 0
	per := make(map[rune]int)
	per['I'] = 1
	per['V'] = 5
	per['X'] = 10
	per['L'] = 50
	per['C'] = 100
	per['D'] = 500
	per['M'] = 1000

	previous := 100000
	for _, value := range s {
		result += per[value]

		if per[value] > previous {
			result -= 2 * previous
		}

		previous = per[value]
	}

	return result
}